package cn.com.elite.impl;

import java.sql.Types;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.elite.algorithm.GUID;
import com.elitecrm.saas.service.EliteSAASException;

public class LoadMeInfoService {
	private static final Log log = LogFactory.getLog(LoadMeInfoService.class);
	
	public String loadMeInfoService(DataSource dataSource,String ep_id,Document doc)throws Exception{
		String ReturnXml="";
		Element SchemeIDEle=(Element)doc.selectSingleNode("/Root/SchemeID");
		if(SchemeIDEle!=null){
			String SchemeID=SchemeIDEle.getText();
			if(SchemeID!=null){
				Element ExamResultMainIDEle=(Element)doc.selectSingleNode("/Root/ExamResultMainID");
				if(ExamResultMainIDEle!=null){
					String ExamResultMainID=ExamResultMainIDEle.getText();
					ReturnXml=loadMeInfoServiceImpl(dataSource,ep_id,SchemeID,ExamResultMainID);
				}else{
					throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamResultMainIDEle==null");
				}
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"SchemeIDEle.getText()==null");
			}
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"SchemeIDEle==null");
		}
		return ReturnXml;
	}
	public String loadMeInfoServiceImpl(DataSource dataSource,String ep_id,String SchemeID,String ExamResultMainID)throws Exception{
		String ReturnXml="";
		Document result=util.getDoc();
		Element root=result.addElement("R");
		root.addElement("result").addText("1");
		if(ExamResultMainID!=null&&!ExamResultMainID.equals("")&&ExamResultMainID.length()>0){
			root.addElement("ExamResultMainID").addText(ExamResultMainID);
		}else{
			GUID guid=new GUID();
			ExamResultMainID=guid.toString();
			log.debug("ExamResultMainID:"+ExamResultMainID);
			root.addElement("ExamResultMainID").addText(ExamResultMainID);
		}
		String W_ExamScheme="W_ExamScheme_"+ep_id;
		String W_ExamList="W_ExamList_"+ep_id;
		String W_QuestionList="W_QuestionList_"+ep_id;
		String W_QuestionType="W_QuestionType_"+ep_id;
		String sql="select "+W_ExamScheme+".ExamMainID,W_QuestionList.QListID,QTypeKey,QDesc,VoicePath,Score " +
				"from "+W_ExamScheme+" " +
				"left join "+W_ExamList+" on "+W_ExamScheme+".ExamMainID="+W_ExamList+".ExamMainID " +
				"left join "+W_QuestionList+" on "+W_ExamList+".QListID="+W_QuestionList+".QListID " +
				"left join "+W_QuestionType+" on "+W_QuestionList+".QType="+W_QuestionList+".QTypeID " +
				"where "+W_ExamScheme+".SchemeID=?";
		log.debug("loadMeInfoServiceImpl_sql:"+sql);
		Object[] params=new Object[]{SchemeID};  
		int[] types=new int[]{Types.VARCHAR};  
		List KSList=util.QueryPersonUseExecute(sql,types,params,dataSource);
		if(KSList!=null&&KSList.size()>0){
			root.addElement("KsresultLEN").addText(KSList.size()+"");
	    	for(int i=0;i<KSList.size();i++){
	    		Map map=(Map)KSList.get(i);
	    		Element kslist=root.addElement("Ksresult");
	    		String QListID=(String)map.get("QListID");
	    		log.debug("QListID:"+QListID);
	    		kslist.addElement("QListID").addText(QListID);
	    		String QTypeKey=(String)map.get("QTypeKey");
	    		log.debug("QTypeKey:"+QTypeKey);
	    		kslist.addElement("QTypeKey").addText(QTypeKey);
	    		String QDesc=(String)map.get("QDesc");
	    		log.debug("QDesc:"+QDesc);
	    		kslist.addElement("QDesc").addText(QDesc);
	    		String VoicePath=(String)map.get("VoicePath");
	    		log.debug("VoicePath:"+VoicePath);
	    		kslist.addElement("VoicePath").addText(VoicePath);
	    		String Score=(String)map.get("Score");
	    		log.debug("Score:"+Score);
	    		kslist.addElement("Score").addText(Score);
	    		String sql1="select choiceindex,choicekey,choiceinfo,picturepath from W_QuestionChoice where QListID=?";
	    		Object[] params1=new Object[]{QListID};  
	    		int[] types1=new int[]{Types.VARCHAR}; 
	    		List KSList1=util.QueryPersonUseExecute(sql1,types1,params1,dataSource);
	    		if(KSList1!=null&&KSList1.size()>0){
	    			for(int m=0;m<KSList1.size();m++){
	    				Element QDesc_list=kslist.addElement("QDesc_list");
	    				Map map1=(Map)KSList1.get(m);
	    				String choiceindex=(String)map1.get("choiceindex");
	    	    		log.debug("choiceindex:"+choiceindex);
	    	    		kslist.addElement("choiceindex").addText(choiceindex);
	    	    		String choicekey=(String)map1.get("choicekey");
	    	    		log.debug("choicekey:"+choicekey);
	    	    		kslist.addElement("choicekey").addText(choicekey);
	    	    		String choiceinfo=(String)map1.get("choiceinfo");
	    	    		log.debug("choiceinfo:"+choiceinfo);
	    	    		kslist.addElement("choiceinfo").addText(choiceinfo);
	    	    		String picturepath=(String)map1.get("picturepath");
	    	    		log.debug("picturepath:"+picturepath);
	    	    		kslist.addElement("picturepath").addText(picturepath);
	    			}	
	    		}
	    	}
		}else{
			root.addElement("KsresultLEN").addText("0");
		}
		ReturnXml=result.asXML();
		log.debug("loadMeInfoServiceImpl_ReturnXml"+ReturnXml);
		return ReturnXml;
		
	}
}
